package com.jwaoo.media.video.helper;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;

import java.io.File;

/**
 * Created by jwaoo on 2017/6/14.
 */

public class VideoHelper {
    // 视频
    protected VideoMessageHelper videoMessageHelper;
    private ICaptureVideoSuccessListener listener;
    private Activity mContext;

    public VideoHelper(Activity context,ICaptureVideoSuccessListener listener){
        mContext = context;
        this.listener = listener;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RequestCode.GET_LOCAL_VIDEO:
                videoHelper().onGetLocalVideoResult(data);
                break;
            case RequestCode.CAPTURE_VIDEO:
                videoHelper().onCaptureVideoResult(data);
                break;
        }
    }

    private VideoMessageHelper videoHelper() {
        if (videoMessageHelper == null) {
            initVideoMessageHelper();
        }
        return videoMessageHelper;
    }

    /**
     * ********************** 视频 *******************************
     */
    public void initVideoMessageHelper() {
        videoMessageHelper = new VideoMessageHelper(mContext, new VideoMessageHelper.VideoMessageHelperListener() {

            @Override
            public void onVideoPicked(File file, String md5) {
                MediaPlayer mediaPlayer = getVideoMediaPlayer(file);
                long duration = mediaPlayer == null ? 0 : mediaPlayer.getDuration();
                int height = mediaPlayer == null ? 0 : mediaPlayer.getVideoHeight();
                int width = mediaPlayer == null ? 0 : mediaPlayer.getVideoWidth();
                listener.onOnSuccess(duration,height,width,file,md5);
            }
        });
    }
    public interface ICaptureVideoSuccessListener{
        /**
         *
         * @param duration 视频时长
         * @param height 视频高
         * @param width 视频宽
         * @param file 视频文件
         * @param md5 视频文件名(MD5 加密后)
         *
         */
        void onOnSuccess(long duration,int height,int width,File file,String md5);
    }
    /**
     * 获取视频mediaPlayer
     * @param file 视频文件
     * @return mediaPlayer
     */
    private MediaPlayer getVideoMediaPlayer(File file) {
        try {
            return MediaPlayer.create(mContext, Uri.fromFile(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void captureVideo(){
        videoHelper().showVideoSource(makeRequestCode(RequestCode.GET_LOCAL_VIDEO), makeRequestCode(RequestCode.CAPTURE_VIDEO));
    }
    protected int makeRequestCode(int requestCode) {
        if ((requestCode & 0xffffff00) != 0) {
            throw new IllegalArgumentException("Can only use lower 8 bits for requestCode");
        }
        //index  位置
//        return ((index + 1) << 8) + (requestCode & 0xff);
        return requestCode;
    }
}
